package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.constant.RedisConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @author zhengjd
 * @date 2020/4/1 13:00
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    SetmealService setmealService;

    @Autowired
    private JedisPool jedisPool;

    /**
     * 分页查询套餐列表
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = setmealService.findPage(queryPageBean);
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 根据套餐id查询检查组的id集合
     */
    @RequestMapping("/findCheckGroupIdBySetmeal")
    public List<Integer> findCheckGroupIdBySetmeal(Integer id) {
        List<Integer> checkGroupIds = setmealService.findCheckGroupIdBySetmeal(id);
        return checkGroupIds;
    }

    /**
     * 将图片上传到七牛云
     */
    @RequestMapping("/upload")
    public Result upload(MultipartFile imgFile){
        Jedis jedis = null;
        try {
            //对获取的文件使用UUID进行文件重命名，防止名字出现重复
            String filename = imgFile.getOriginalFilename();
            int lastIndex = filename.lastIndexOf(".");
            //获取文件的格式名
            String suffix = filename.substring(lastIndex);
            filename = UUID.randomUUID().toString() + suffix;
            //将图片上传到七牛云中
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),filename);
            //若上传成功，则将图片名字存入Redis中
            jedis = jedisPool.getResource();
            jedis.sadd(RedisConstant.SETMEAL_PIC_RESOURCES,filename);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,filename);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    /**
     *添加套餐
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        Jedis jedis = null;
        try {
            setmealService.add(setmeal,checkgroupIds);
            //如果添加套餐成功，则将图片信息存入set集合（已经添加成功的集合）中
            jedis = jedisPool.getResource();
            jedis.sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
            return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        } finally {
            if (jedis != null) {
                jedis.close();
            }

        }
    }

    /**
     * 根据id查询套餐
     */
    @RequestMapping("/findById")
    public Setmeal findById(Integer id){
        try {
            Setmeal setmeal = setmealService.findById(id);
            return setmeal;
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     *编辑套餐
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        try {
            setmealService.edit(setmeal,checkgroupIds);
            return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }

    /**
     * 删除套餐
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            setmealService.delete(id);
            return new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
        }catch (RuntimeException e) {
            return new Result(false,e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_SETMEAL_FAIL);
        }
    }




}
